<?php

namespace App\Http\Controllers\Campaign\Platform;

use App\Http\Controllers\BaseApiController;
use App\Services\Campaign\Platform\RegService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;



class RegController extends BaseApiController
{
    private $service;
    private $result=[];
    //构造方法
    public function __construct(RegService $service)
    {
        //继承基类的构造方法
        parent::__construct();
        //依赖注入service
        $this->service=$service;
    }
    public function aaa()
    {
        echo 1;
    }



    /**
     * 发送验证码的方法
     * 如果是注册   就需要  手机号
     * 如果是忘记密码   需要手机号  还有类型（1）
     */
    public function Sendcode(Request $request){
//        echo 123;
        //验证手机号是否存在
        $rules=[
            'tel'=>'required|size:11'
        ];
        $error=$this->validate($rules,$request);
        if($error){
            return $error;
        }

        if(!is_numeric($request['tel'])){
            return $this->fail(113);
        }

//        如果存在 去发送短信
        $data=$this->service->SendCode($request);

        //验证码  发送成功
        if($data=='200'){
            return $this->success();
        }else{
            return $this->fail($data);
        }
//        return $data;
    }


    /**
     * 注册用户
     */
    public function RegUser(Request $request){
        $resule=[
            'tel'=>'required|size:11',
            'pwd'=>'required|string',
            'code'=>'required'
        ];
        $error=$this->validate($resule,$request);
        if($error){
            return $error;
        }
        //手机号必须为数字
        if(!is_numeric($request['tel'])){
            return $this->fail(113);
        }
        //验证码必须为数字
        if(!is_numeric($request['code'])){
            return $this->fail(114);
        }

        //注册

        $data=$this->service->RegUsers($request);

        if($data==200){
            return $this->success();
        }else{
            return $this->fail($data);
        }

    }


    /**
     * 忘记密码
     */
    public function ForgetPwd(Request $request){
        $resule=[
            'tel'=>'required|size:11',
            'pwd'=>'required|string',
            'code'=>'required'
        ];
        $error=$this->validate($resule,$request);
        if($error){
            return $error;
        }
        //手机号必须为数字
        if(!is_numeric($request['tel'])){
            return $this->fail(113);
        }
        //验证码必须为数字
        if(!is_numeric($request['code'])){
            return $this->fail(114);
        }



        //去修改密码
        $data=$this->service->ForgetPwds($request);
//        return $data;
        if($data==200){
            return $this->success();
        }else{
            return $this->fail($data);
        }
    }


}
